package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.MessageListAdapter;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.base.RefreshCallback;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.MessageListBean;
import com.example.wanbaqian.customView.swipeLayout.SwipeDeleteCallback;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 消息列表
 */
public class MessageListActivity extends BaseActivity implements HttpCallback, RefreshCallback, MultiItemTypeAdapter.OnItemClickListener, SwipeDeleteCallback {
    @BindView(R.id.MessageList_RecyclerView)
    RecyclerView MessageListRecyclerView;
    private MessageListAdapter adapter;
    private List<MessageListBean> listBeen = new ArrayList<>();
    private EmptyWrapper emptyWrapper;

    @Override
    protected int getLayout() {
        return R.layout.activity_message_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.main_color);
        setToolBar("消息中心");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setHeaderAndFooter(R.id.MessageList_SpringView, true, true, this);
        MessageListRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new MessageListAdapter(mContext, R.layout.item_message_list, listBeen);
        adapter.setOnItemClickListener(this);
        adapter.setDeleteCallBack(this);
        emptyWrapper = new EmptyWrapper(adapter);
        emptyWrapper.setEmptyView(R.layout.view_empty_message);
        MessageListRecyclerView.setAdapter(emptyWrapper);
        getDate();
    }

    private void getDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.MESSAGE_LIST, mContext,
                new String[]{"accessid", "cur_page", "page_size"}
                , new String[]{SPUtils.getAccessid(mContext), page + "", count + ""}),
                mContext, HttpURL.MESSAGE_LIST, CommonValue.REQUEST_CODE1, this);
    }

    @Override
    protected void initDate() {
    }

    private static final String TAG = "MessageListActivity";

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                springView.onFinishFreshAndLoad();
                if (page == 1) {
                    listBeen.clear();
                }
                listBeen.addAll(JsonUtils.getInstance().jsonToList(JsonUtils.getInstance().getMessageList(content), MessageListBean.class));
//                listBeen.add(new MessageListBean("1313", "131313131", "1", "131531", "5165461313"));
//                Log.i(TAG, "onSuccess: " + listBeen.size());
                emptyWrapper.notifyDataSetChanged();
                canLoadmore = JsonUtils.getInstance().havePage(content);
                break;
            case CommonValue.REQUEST_CODE2:
                listBeen.remove(deletePosition);
                emptyWrapper.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }

    @Override
    public void getNetDate() {
        getDate();
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    private int deletePosition = -1;

    @Override
    public void deleteListeren(int position) {
        Log.i(TAG, "deleteListeren: " + position);
        deletePosition = position;
        deleteMessage(listBeen.get(position).getMessage_id());
    }

    /**
     * 删除消息
     */
    private void deleteMessage(String message_id) {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.MESSAGE_DELETE, mContext,
                new String[]{"accessid", "message_id"}
                , new String[]{SPUtils.getAccessid(mContext), message_id}),
                mContext, null, CommonValue.REQUEST_CODE2, this);
    }
}
